package com.lottery.material.controller.portal.banner;

import com.lottery.material.controller.base.BaseController;
import com.lottery.material.enumeration.ResultEnum;
import com.lottery.material.resolver.CustomException;
import com.lottery.material.service.portal.banner.BannerConfService;
import com.lottery.material.util.ExceptionUtil;
import com.lottery.material.util.ParameterUtil;
import com.lottery.material.util.ResponseResult;
import com.lottery.material.vo.portal.banner.BannerVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Description:    轮播图Controller
 * Author:         sunny
 * CreateDate:     2019-04-18 17:43
 */
@RequestMapping("/portal/banner")
@Slf4j
@Controller
public class PortalBannerController extends BaseController {

    @Autowired
    private BannerConfService bannerConfService;

    /**
     * description: 获取轮播图
     * author sunny
     * createDate 2019-04-18 下午 5:45
     * @param request
     * @return
     */
    @RequestMapping(value = "/getBannerList", method = RequestMethod.POST)
    @ResponseBody
    public ResponseResult goldPayTips(HttpServletRequest request) {
        Map<String, String> reqParamMap = new HashMap<>();
        //解析参数
        if (ParameterUtil.analyticParameter(request, reqParamMap)) {
            throw new CustomException(ResultEnum.PARSE_EXCEPTION);
        }

        //非空判断
        ResponseResult respDate = validateEmpty(reqParamMap);
        if (null != respDate) {
            return respDate;
        }

        String menuCode = reqParamMap.get("menuCode");
        if (StringUtils.isBlank(menuCode)) {
            return ResponseResult.buildFormatResult(ResultEnum.PARAMETER_NOT_NULL, "menuCode");
        }

        //签名验证
        ResponseResult responseResult = ParameterUtil.checkSign(reqParamMap);
        if (null != responseResult) {
            return responseResult;
        }

        try {
            //查询banner封装成VO
            List<BannerVo> bannerList = bannerConfService.findBannerVoByMenuCode(menuCode);
            return ResponseResult.buildResult(ResultEnum.SUCCESS, bannerList);
        } catch (Exception e) {
            log.error("【获取轮播图出现异常】:{}", ExceptionUtil.logExceptionStack(e));
            throw new CustomException(ResultEnum.GET_BANNER_LIST_EXCEPTION);
        }
    }

}
